<template>
  <base-breadcrumb>
    <a-row
      :gutter="10"
      class="page-height"
    >
      <!-- 树搜索 -->
      <a-col
        :span="6"
        class="permission-collapsed"
        :class="{ 'permission-tree-collapsed': collapsed }"
      >
        <a-card
          :style="{ border: collapsed ? 'none' : 'border: 1px solid #e8e8e8' }"
          class="page-height-tree"
        >
          <title-name title="组织架构" />
          <PermissionTreeOrganization
            :collapsed.sync="collapsed"
            @select="handleSelect"
          />
        </a-card>
      </a-col>
      <a-col
        :span="18"
        class="permission-collapsed  page-height-list-wrapper"
        :style="collapsed ? 'width:calc(100% - 10px)' : ''"
      >
        <searchData
          :search-data="searchArr"
          @search="search"
        />

        <a-card
          :bordered="false"
          class="page-height-list"
        >
          <!--          <title-name title="业主登录信息列表" />-->

          <div class="table-operator">
            <a-button
              v-btnPermission="'pm_033_add_01'"
              type="primary"
              @click="handleAdd"
            >
              新增业主登录信息
            </a-button>
          </div>

          <div class="pub-table-select-count">
            <a-icon
              type="info-circle"
              theme="twoTone"
            />
            <span style="margin-right: 12px">
              业主登录信息总数：<span class="totalNum">{{ totalDels.totalNum }} 个</span> | 可用：<span class="totalNum">{{ totalDels.availableNum }} 个</span> | 不可用：<span
                class="totalNum"
              >{{ totalDels.disabledNum }} 个</span>
            </span>
          </div>

          <list-table
            ref="table"
            size="default"
            :row-key="(record) => record.id"
            :columns="columns"
            :request="loadData"
            show-size-changer
            show-quick-jumper
            show-pagination="auto"
          >
            <span
              slot="serial"
              slot-scope="text, record, index"
            >{{ index + 1 }}</span>
            <span
              slot="action"
              slot-scope="text, record, index"
            >
              <MoreButton>
                <a
                  v-btnPermission="'pm_033_loo_02'"
                  @click="handleSub(record)"
                >查看</a>
                <a-divider
                  v-btnPermission="'pm_033_edi_03'"
                  type="vertical"
                />
                <a
                  v-btnPermission="'pm_033_edi_03'"
                  @click="handleEdit(record)"
                >编辑</a>
                <a-divider
                  v-btnPermission="'pm_033_res_04'"
                  type="vertical"
                />
                <a
                  v-btnPermission="'pm_033_res_04'"
                  @click="resetPwd(record)"
                >重置密码</a>
              </MoreButton>
            </span>
          </list-table>

          <!--重置密码-->
          <a-modal
            title="二次授权"
            :visible="visiblePwd"
            :confirm-loading="confirmLoading"
            destroy-on-close
            @ok="resetUserPwdReq"
            @cancel="handleCancelPwd"
          >
            <a-form-model
              ref="ruleForm"
              :model="formPwd"
              :rules="pwdRule"
              :label-col="{ lg: { span: 6 }, sm: { span: 6 } }"
              :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
            >
              <a-row :gutter="24">
                <a-col
                  :md="24"
                  :sm="24"
                >
                  <a-form-model-item
                    label="登录密码"
                    prop="authPwd"
                  >
                    <a-input-password
                      v-model.trim="formPwd.authPwd"
                      type="password"
                      placeholder="请输入登录密码"
                    >
                      <a-icon
                        slot="prefix"
                        type="lock"
                        style="color: rgba(0, 0, 0, 0.25)"
                      />
                    </a-input-password>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
          </a-modal>

          <!--新增账号-->
          <a-modal
            v-model="visible"
            width="800px"
            :title="title"
            destroy-on-close
          >
            <template slot="footer">
              <a-button
                key="back"
                @click="handleCancel"
              >
                取消
              </a-button>
              <a-button
                key="submit"
                :loading="loading"
                type="primary"
                @click="handleOk"
              >
                确定
              </a-button>
            </template>
            <a-form-model
              ref="form1"
              :model="form"
              :rules="rules"
            >
              <a-row>
                <a-col
                  :md="24"
                  :sm="24"
                >
                  <a-form-model-item
                    label="所属单位"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="projectId"
                  >
                    <org-tree-select
                      is-need-auth
                      check-strictly
                      is-auto-select-first
                      v-model="form.projectId"
                      @select="selectOrg"
                      @getProjectInfo="getProjectInfo"
                    />
                  </a-form-model-item>
                </a-col>
                <!--                <a-col-->
                <!--                  :md="12"-->
                <!--                  :sm="12"-->
                <!--                >-->
                <!--                  <a-form-model-item-->
                <!--                    label="分公司"-->
                <!--                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"-->
                <!--                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"-->
                <!--                    prop="branchId"-->
                <!--                  >-->
                <!--                    <SelectSubCompany-->
                <!--                      v-model="form.branchId"-->
                <!--                      placeholder="请选择"-->
                <!--                      @change="clearData"-->
                <!--                    />-->
                <!--                  </a-form-model-item>-->
                <!--                </a-col>-->
                <!--                <a-col-->
                <!--                  :md="12"-->
                <!--                  :sm="12"-->
                <!--                >-->
                <!--                  <a-form-model-item-->
                <!--                    label="项目名称"-->
                <!--                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"-->
                <!--                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"-->
                <!--                    prop="projectId"-->
                <!--                  >-->
                <!--                    <SelectProject-->
                <!--                      v-model="form.projectId"-->
                <!--                      placeholder="请选择"-->
                <!--                      :is-clear="true"-->
                <!--                      :form="form"-->
                <!--                      key-value="branchId"-->
                <!--                      @projects="projectsData"-->
                <!--                    />-->
                <!--                  </a-form-model-item>-->
                <!--                </a-col>-->
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="项目编号"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="form.projectNum"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="建设单位名称"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="form.companyName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="建设单位性质"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="form.natureName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="建设单位统一社会信用代码"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="form.creditCode"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="登录人姓名"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="loginName"
                  >
                    <BaseInput
                      v-model="form.loginName"
                      placeholder="输入登录人姓名"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="登录账号(电话号码)"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="loginAccount"
                  >
                    <BaseInput
                      v-model="form.loginAccount"
                      placeholder="输入登录人账号"
                      @blur="contractPhone"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row v-if="isShowPwd == 1">
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="登录密码"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="loginPassword"
                  >
                    <BaseInput
                      v-model="form.loginPassword"
                      type="password"
                      disabled
                      :max-length="16"
                      placeholder="输入登录密码"
                      @blur="getPwd(form.loginPassword)"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="确认登录密码"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="configloginPassword"
                  >
                    <BaseInput
                      v-model="form.configloginPassword"
                      type="password"
                      disabled
                      :max-length="16"
                      placeholder="输入确认登录密码"
                      @blur="configPwd"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="建设单位项目负责人姓名"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="principalName"
                  >
                    <BaseInput
                      v-model="form.principalName"
                      placeholder="输入建设单位项目负责人姓名"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="创建人"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="reasonCode"
                  >
                    <BaseInput
                      v-model="form.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="创建时间"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="form.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="状态"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="enabled"
                  >
                    <a-select
                      v-model="form.enabled"
                      allow-clear
                      placeholder="请选择"
                    >
                      <a-select-option :value="1">
                        可用
                      </a-select-option>
                      <a-select-option :value="0">
                        不可用
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
          </a-modal>

          <!--查看账号-->
          <a-modal
            v-model="visibles"
            width="800px"
            :title="'查看业主登录信息'"
          >
            <template slot="footer">
              <a-button
                key="back"
                @click="handleCancels"
              >
                取消
              </a-button>
            </template>
            <a-form-model
              ref="form2"
              :model="form"
              :rules="rules"
            >
              <a-row>
                <a-col :span="24">
                  <a-form-model-item
                    label="所属单位"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <org-tree-select
                      disabled
                      :value="forms.projectId"
                    />
                  </a-form-model-item>
                </a-col>
                <!--                <a-col-->
                <!--                  :md="12"-->
                <!--                  :sm="12"-->
                <!--                >-->
                <!--                  <a-form-model-item-->
                <!--                    label="分公司"-->
                <!--                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"-->
                <!--                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"-->
                <!--                    prop="branchId"-->
                <!--                  >-->
                <!--                    <BaseInput-->
                <!--                      v-model="forms.branchName"-->
                <!--                      disabled-->
                <!--                    />-->
                <!--                  </a-form-model-item>-->
                <!--                </a-col>-->
                <!--                <a-col-->
                <!--                  :md="12"-->
                <!--                  :sm="12"-->
                <!--                >-->
                <!--                  <a-form-model-item-->
                <!--                    label="项目名称"-->
                <!--                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"-->
                <!--                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"-->
                <!--                    prop="projectId"-->
                <!--                  >-->
                <!--                    <BaseInput-->
                <!--                      v-model="forms.projectName"-->
                <!--                      disabled-->
                <!--                    />-->
                <!--                  </a-form-model-item>-->
                <!--                </a-col>-->
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="项目编号"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="forms.projectNum"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="建设单位名称"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="forms.companyName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="建设单位性质"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="forms.natureName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="建设单位统一社会信用代码"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="forms.creditCode"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="登录人姓名"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="loginName"
                  >
                    <BaseInput
                      v-model="forms.loginName"
                      placeholder="输入登录人姓名"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="登录账号(电话号码)"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="loginAccount"
                  >
                    <BaseInput
                      v-model="forms.loginAccount"
                      placeholder="输入登录人账号"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="登录密码"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="loginPassword"
                  >
                    <BaseInput
                      v-model="forms.loginPassword"
                      placeholder="输入登录密码"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="确认登录密码"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="forms.configloginPassword"
                      placeholder="输入确认登录密码"
                      disabled
                      @blur="configPwd"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="建设单位项目负责人姓名"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="principalName"
                  >
                    <BaseInput
                      v-model="forms.principalName"
                      placeholder="输入建设单位项目负责人姓名"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="创建人"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="reasonCode"
                  >
                    <BaseInput
                      v-model="forms.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="创建时间"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                  >
                    <BaseInput
                      v-model="forms.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  :md="12"
                  :sm="12"
                >
                  <a-form-model-item
                    label="状态"
                    :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                    :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                    prop="enabled"
                  >
                    <BaseInput
                      v-model="forms.enabledName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
          </a-modal>
        </a-card>
      </a-col>
    </a-row>
  </base-breadcrumb>
</template>

<script>
import { resetUserPwd } from '@/api/commons.js'
import { STable } from '@/components'
import { getLoginsList, getLoginTotal, addLogins, editLogins, getLoginsOne } from '@/api/project/loginInfo'
import { updataUserManagePsw } from '@/api/system/system'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/SelectProjects/SelectProject' // 项目
import { encryptFromText } from '@/tool/sm.js'
import { clientId, clientSecret, PublicKey } from '@/config'
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
import ARow from 'ant-design-vue/es/grid/Row'
import { getAuditProject } from '@/api/themeChoose/themeChoose'

const columns = [
  {
    title: '序号',
    width: 65,

    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '建设单位名称(业主)',
    dataIndex: 'companyName',
    ellipsis: true,
    width: 200
  },
  {
    title: '建设单位性质',
    dataIndex: 'natureName',
    ellipsis: true,
    width: 200
  },
  {
    title: '建设单位统一社会信用代码',
    width: 200,
    dataIndex: 'creditCode'
  },
  {
    title: '建设单位项目负责人姓名',
    width: 200,
    dataIndex: 'principalName'
  },
  {
    title: '登录人姓名',
    width: 100,
    dataIndex: 'loginName'
  },
  {
    title: '登录账号(电话号码)',
    width: 140,
    dataIndex: 'loginAccount'
  },
  {
    title: '项目',
    dataIndex: 'projectName',
    ellipsis: true,
    width: 200
  },
  {
    title: '分公司',
    width: 90,
    dataIndex: 'branchName'
  },
  {
    title: '项目编号',
    width: 100,
    dataIndex: 'projectNum'
  },
  {
    title: '创建人',
    width: 90,
    dataIndex: 'creatorName'
  },
  {
    title: '创建时间',
    width: 100,
    dataIndex: 'createTime'
  },
  {
    title: '状态',
    width: 100,
    dataIndex: 'enabledName'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 200,
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'a' + Date.now(),
  components: {
    ARow,
    STable,
    SelectSubCompany,
    SelectProject,
    PermissionTreeOrganization
  },
  data() {
    const validatePass = (rule, value, callback) => {
      const reg = /([a-zA-Z0-9!@#$%^&*()_?<>{}]){6,18}/
      if (!reg.test(value)) {
        callback(new Error('请输入6到16位密码'))
      } else {
        callback()
      }
    }
    return {
      columns,
      visiblePwd: false,
      confirmLoading: false,
      collapsed: false,
      select: {},
      pwdRule: {
        authPwd: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { validator: validatePass, trigger: 'blur' }
        ]
      },
      formPwd: {
        authPwd: '' // 授权密码（为当前登录用户的密码）
      },
      isShowPwd: 1,
      searchArr: [
        // 搜索框
        {
          name: '业主名称',
          value: 'ownerName',
          type: 'input'
        },
        // {
        //   name: '项目名称',
        //   value: 'projectName',
        //   type: 'input'
        // },
        // {
        //   name: '分公司',
        //   valueKey: 'branchComId',
        //   type: SelectSubCompany
        // },
        {
          name: '项目编号',
          value: 'projectCode',
          type: 'input'
        },
        {
          name: '创建时间',
          value: 'createdTime',
          type: 'dateRange'
        }
      ],

      loading: false,
      rules: {
        branchId: [{ required: true, message: '请选择分公司', trigger: 'change' }],
        projectId: [{ required: true, message: '请选择项目', trigger: 'input' }],
        loginName: [{ required: true, message: '请输入登录人姓名', trigger: 'blur' }],
        loginAccount: [{ required: true, message: '请输入登录账号(电话号码)', trigger: 'blur' }],
        loginPassword: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
        configloginPassword: [{ required: true, message: '请输入确认登录密码', trigger: 'blur' }],
        principalName: [{ required: true, message: '请输入建设单位项目负责人姓名', trigger: 'blur' }],
        enabled: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      },
      visible: false,
      visibles: false,
      title: '',

      form: {
        projectId: undefined,
        loginPassword: '********',
        configloginPassword: '********'
      },

      forms: {},

      queryParam: {},

      data: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const obj = {}
        // if (!this.select) {
        //   return
        // }
        // 二级-分公司id
        if (this.select?.tageCode === 'BC') {
          obj.branchComId = this.select.orgId
        }
        // 三级-项目id
        if (this.select?.tageCode === 'PD') {
          obj.projectId = this.select.projectId
        }
        if (this.queryParam?.createdTime && this.queryParam?.createdTime.length > 0) {
          this.queryParam.createTimeStart = this.queryParam.createdTime[0].format('YYYY-MM-DD')
          this.queryParam.createTimeEnd = this.queryParam.createdTime[1].format('YYYY-MM-DD')
          delete this.queryParam.createdTime
        }
        const requestParameters = Object.assign({}, parameter, { ...this.queryParam, ...obj })
        return getLoginsList(requestParameters).then((res) => {
          return res
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      points: [],
      totalDels: {},

      modelPwd: {},

      ids: [] //删除ids
    }
  },

  created() {
    this.getLoginTotal()
  },
  computed: {
    // rowSelection() {
    //   return {
    //     selectedRowKeys: this.selectedRowKeys,
    //     selectedRows: this.selectedRows,   // 当前行所有数据
    //     onChange: this.onSelectChange
    //   }
    // }
  },
  methods: {
    selectOrg(val) {
      if(val&&val.length) {
        this.$refs.form1.clearValidate(['projectId'])
        this.form.projectId = val[val.length - 1].projectId
        this.form.branchId = val[val.length - 2].orgId
        this.form.branchName = val[val.length - 2].orgName
      }
    },
    getProjectInfo(val) {
      this.projectsData(val)
    },
    handleSelect(node) {
      this.select = node
      this.search()
    },
    /**
     * 重置密码
     */
    async resetUserPwdReq() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) {
          return
        }
        try {
          if (this.confirmLoading) {
            return
          }
          this.confirmLoading = true
          const params = {
            sign: encryptFromText(this.modelPwd.userId),
            pscode: this.doSm3AndSm2Encrypt(this.formPwd.authPwd),
            userId: this.modelPwd.userId
          }
          await resetUserPwd(params)
          this.visiblePwd = false
          this.$message.success('重置成功')
          this.formPwd = {
            authPwd: ''
          }
        } catch (e) {
          console.error(e)
          e && this.$message.error(e.message || '请求失败，请稍后重试！')
        }
        this.confirmLoading = false
      })
    },
    doSm3AndSm2Encrypt(sourceStr) {
      // 密码加密
      /* eslint-disable */
      const sm2Utils = new Sm2Utils()
      const public_key = PublicKey
      const sm3_random_plain = Sm3Utils.encryptFromText(sourceStr) + '|' + sm2Utils.randomWord(8) + '|' + sourceStr
      const sm3_sm2_plain = sm2Utils.encryptFromText(public_key, sm3_random_plain)
      return '{crypto}' + sm3_sm2_plain
      /* eslint-enable */
    },

    handleCancelPwd() {
      this.visiblePwd = false
      this.formPwd = {
        authPwd: ''
      }
    },
    // 重置密码
    resetPwd(val) {
      this.visiblePwd = true
      this.modelPwd = val
    },
    getPwd(val) {
      if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(val)) {
        this.$message.error('密码必须由6-16位字母、数字组成，区分大小写！')
        return false
      }
    },
    configPwd(val) {
      if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(val.target.value)) {
        this.$message.error('密码必须由6-16位字母、数字组成，区分大小写！')
        return false
      }
      if (this.form.loginPassword != val.target.value) {
        this.$message.error('两次输入密码不一致，核对后请重新输入！')
        return
      }
    },
    // 获取项目信息
    projectsData(val) {
      this.form.projectNum = val.projectNum
      this.form.companyName = val.constructionName
      this.form.natureName = val.constructionNatureName
      this.form.creditCode = val.constructionCode
      this.$forceUpdate()
    },

    // 清空选中信息
    clearData() {
      this.form.projectNum = ''
      this.form.companyName = ''
      this.form.natureName = ''
      this.form.creditCode = ''
    },

    contractPhone() {
      if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.form.loginAccount)) {
        this.$message.error('电话号码有误，核对后请重新输入')
        return false
      }
    },

    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },

    // 搜索
    search(val) {
      this.queryParam = val
      this.$refs.table.refresh(true)
      this.getLoginTotal()
    },

    // 统计
    getLoginTotal() {
      const obj = {}
      // 二级-分公司id
      if (this.select.tageCode === 'BC') {
        obj.branchComId = this.select.orgId
      }
      // 三级-项目id
      if (this.select.tageCode === 'PD') {
        obj.projectId = this.select.projectId
      }
      const requestParameters = Object.assign({}, { ...this.queryParam, ...obj })
      getLoginTotal(requestParameters).then((res) => {
        this.totalDels = res.data
      })
    },

    handleAdd() {
      this.title = '新增业主登录信息'
      this.form = {}
      this.isShowPwd = 1
      this.form.creatorName = JSON.parse(sessionStorage.getItem('ycloud-user_info')).name
      this.form.createTime = this.getNowDate()
      this.form.loginPassword = 'CscecXn*2020'
      this.form.configloginPassword = 'CscecXn*2020'

      this.visible = true
    },

    // 查看
    handleSub(record) {
      this.isShowPwd = 0
      this.visibles = true
      this.forms = { ...record }
      this.forms.configloginPassword = record.loginPassword
    },

    // 编辑
    handleEdit(record) {
      this.isShowPwd = 0
      this.title = '编辑业主登录信息'
      this.visible = true
      this.$nextTick(() => {
        this.form.projectId = record.projectId
      })
      this.form = { ...record }
      this.form.configloginPassword = record.loginPassword
    },

    // 关闭弹层
    handleCancel() {
      this.visible = false
    },
    // 关闭弹层
    handleCancels() {
      this.visibles = false
    },

    // 确认业主登录信息
    handleOk() {
      let that = this
      if (that.loading) return

      that.$refs.form1.validate((err, value) => {
        if (err) {
          if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(that.form.loginAccount)) {
            that.$message.error('电话号码有误，核对后请重新输入')
            return false
          }

          if (that.form.id) {
            that.loading = true
            that.form.loginPassword = null
            editLogins(that.form)
              .then((res) => {
                that.loading = false
                if (res.code == 200 || res.data.code == 200) {
                  that.$message.success('编辑成功')
                  that.visible = false
                  that.$refs.table.refresh(true)
                  that.getLoginTotal()
                }
              })
              .catch(() => {
                that.loading = false
              })
          } else {
            if (that.form.loginPassword != that.form.configloginPassword) {
              that.$message.error('请确认两次密码是否一致，核对后请重新输入！')
              return
            }

            that.loading = true
            addLogins(that.form)
              .then((res) => {
                that.loading = false
                if (res.code == 200 || res.data.code == 200) {
                  that.$message.success('新增成功')
                  that.visible = false
                  that.$refs.table.refresh(true)
                  that.getLoginTotal()
                }
              })
              .catch(() => {
                that.loading = false
              })
          }
        }
      })
    },

    // 获取扣分分值对象值
    getExtraData(val) {
      const target = this.points.find((item) => item.deductScoreId === val)
      this.form.deductScore = target.deductScoreCode
      this.form.deductScoreName = target.deductScore
      this.form.status = target.status
    }
  }
}
</script>




